package ltesim.editors;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;

import ltesim.xmlParser.XMLParser;
import ltesim.xmlParser.xmlData.XMLDataset;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.ScrolledForm;

/**
 * Base class for the editor page<br/>
 * Extend this class for making another Form-based XML Editor
 * 
 * @author xirka
 * 
 */
public class LEditorPage extends FormPage {

	protected XMLParser parser;
	private LEditor lEditor;
	private boolean dirty;
	protected LFormModifyListener dirtyStateListener = new LFormModifyListener(
			this);

	private transient PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
			this);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.editor.FormPage#isDirty()
	 */
	@Override
	public boolean isDirty() {
		return dirty;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.forms.editor.FormPage#doSave(org.eclipse.core.runtime.
	 * IProgressMonitor)
	 */
	@Override
	public void doSave(IProgressMonitor monitor) {
		System.out.println("doSave");
		// super.doSave(monitor);
		XMLDataset newData = getXMLData();
		parser.setXMLData(newData);
		try {
			parser.writeXML();
		} catch (IOException ex) {
			System.err.println("Error in writing XML");
			ex.printStackTrace();
		}
		setDirty(false);
		fireDirtyStateChange();
	}

	/**
	 * Set dirty state
	 * 
	 * @param dirty
	 */
	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}

	/**
	 * Refresh by reloading the opened file
	 */
	public void refresh() {
		parser.refresh();
		setXMLData(parser.getXMLData());
		setDirty(false);
		fireDirtyStateChange();
	}

	/**
	 * addPropertyChangeListener
	 * 
	 * @param propertyName
	 * @param listener
	 */
	public void addPropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
	}

	/**
	 * removePropertyChangeListener
	 * 
	 * @param propertyName
	 * @param listener
	 */
	public void removePropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		propertyChangeSupport.removePropertyChangeListener(propertyName,
				listener);
	}

	/**
	 * firePropertyChange
	 * 
	 * @param propertyName
	 *            Name of the property
	 * @param oldValue
	 *            Old value of the property
	 * @param newValue
	 *            New value of the property
	 */
	protected void firePropertyChange(String propertyName, Object oldValue,
			Object newValue) {
		propertyChangeSupport.firePropertyChange(propertyName, oldValue,
				newValue);
	}

	/**
	 * Toggle the dirtyState
	 */
	public void fireDirtyStateChange() {
		firePropertyChange("dirtyState", !isDirty(), isDirty());
	}

	/**
	 * Constructor
	 * 
	 * @param _editor
	 */
	public LEditorPage(LEditor _editor) {
		super(_editor, "firstPage", "LTE Simulator Configuration");
		lEditor = _editor;
		parser = lEditor.getxParser();
		dirty = false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.forms.editor.FormPage#createFormContent(org.eclipse.ui
	 * .forms.IManagedForm)
	 */
	@Override
	protected void createFormContent(IManagedForm managedForm) {
		ScrolledForm form = managedForm.getForm();
		form.setText("Dummy Content; please override createFormContent method");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.editor.FormPage#dispose()
	 */
	@Override
	public void dispose() {
		super.dispose();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.editor.FormPage#setFocus()
	 */
	@Override
	public void setFocus() {
	}

	/**
	 * Set XMLDataset _data to current parser
	 * 
	 * @param _data
	 */
	public void setXMLData(XMLDataset _data) {
		parser.setXMLData(_data);
	}

	/**
	 * Get the current parsed data
	 * 
	 * @return Parseddata in XMLDataset container
	 */
	public XMLDataset getXMLData() {
		return parser.getXMLData();
	}

	/**
	 * Get the editor parser class
	 * 
	 * @return XMLParser
	 */
	public XMLParser getXMLParser() {
		return parser;
	}

}
